﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            BasicConcept();
            QuerySyntax();
            QueryOperations();
            Console.ReadLine();
        }

        private static void BasicConcept()
        {
            //Linq: Language Intergarted Query
            //实现了IEnumerable接口的都可以使用
            // LINQ to SQL, LINQ to XML, LINQ to DataSet, LINQ to Objects
            int[] numbers = { 5, 10, 8, 3, 6, 12 };
            //1、Query syntax
            var numQuery1 = from num in numbers where num % 2 == 0 orderby num select num;

            //2、Method syntax
            var numQuery2 = numbers.Where(n => n % 2 == 0).OrderBy(n => n);

            foreach (var i in numQuery1)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
        }

        private static void QuerySyntax()
        {
            //1、Data Source  数组已经实现了IEnumerable接口
            int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7 };

            //2、Query creation
            var numQuery = from num in numbers where num % 2 == 0 select num;

            //3、Query execution
            foreach (var num in numQuery)
            {
                Console.Write("{0,1}", num);
            }
            Console.WriteLine();

            //强制执行Query语句
            int numCount = numQuery.Count();
            numQuery.ToList();
            numQuery.ToArray();
        }

        private static void QueryOperations()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer() { Name = "Jack", City = "Beijing" });
            customers.Add(new Customer() { Name = "LiLei", City = "Beijing" });
            customers.Add(new Customer() { Name = "Wangmeimei", City = "Shanghai" });

            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee() { Name = "Jack", ID = 101 });
            employees.Add(new Employee() { Name = "Emily", ID = 102 });


            //group
            var queryCustomers = from c in customers group c by c.City;

            foreach (var cg in queryCustomers)
            {
                Console.WriteLine(cg.Key);
                foreach (var c in cg)
                {
                    Console.WriteLine(" {0}", c.Name);
                }
            }
            //into
            var queryCustomersAndGroup = from c in customers group c by c.City into cusGroup where cusGroup.Count() >= 2 select new { City = cusGroup.Key, Number = cusGroup.Count() };

            foreach (var c in queryCustomersAndGroup)
            {
                Console.WriteLine("{0} Count {1}", c.City, c.Number);
            }

            //join
            var queryJoin = from c in customers
                            join e in employees on c.Name equals e.Name
                            select new { PersonName = c.Name, PersonID = e.ID, PerysionCity = c.City };

            foreach (var p in queryJoin)
            {
                Console.WriteLine("{0} {1} {2}", p.PersonName, p.PersonID, p.PerysionCity);
            }

            //let 存储中间过程的一个变量
            string[] strings = { "Hello dunso", "This is Friday", "Are you happy?" };
            var stringQuery = from s in strings let words = s.Split(' ') from word in words let w = word.ToUpper() select w;

            foreach(var s in stringQuery)
            {
                Console.WriteLine("{0} ", s);
            }

        }
    }
    class Customer
    {
        public string Name
        {
            get;
            set;
        }

        public string City
        {
            get;
            set;
        }
    }

    class Employee
    {
        public string Name { get; set; }
        public int ID { get; set; }

    }
}
